package engine.config;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class ConfigReader // NOTE: Cannot read/write more than one file at a time
{
    
    private static final String FILE_PATH = "config/";
    private static final String FILE_TYPE = ".cfg";
    
    
    private BufferedReader reader;
    private HashMap<String, String> map;
    
    public ConfigReader(String filename) 
    {
        this.map = new HashMap<>();
        try
        {
            this.reader = new BufferedReader(
                new FileReader(
                    ConfigReader.FILE_PATH + filename + ConfigReader.FILE_TYPE
                )
            );
            String line = this.reader.readLine();
            while (line != null)
            {
                String[] splitName_Data = line.split("=");
                String name = splitName_Data[0];
                String data = splitName_Data[1];
                this.map.put(name, data);
                line = this.reader.readLine();
            }
        }
        catch (FileNotFoundException e)
        {
            System.out.println(e);
            System.out.println("[Engine]  File: " + filename + " Not Found.");
            System.exit(1);
        }
        catch (IOException e)
        {
            System.out.println(e);
            System.out.println("[Engine]  Failure reading a file: " + filename);
            System.exit(1);
        }
    }
    
    //<editor-fold defaultstate="collapsed" desc="readBool/Int/Float/Str">
    public boolean readBool(String name)
    {
        return Boolean.valueOf(this.readStr(name));
    }
    
    public int readInt(String name)
    {
        return Integer.valueOf(this.readStr(name));
    }
    
    public float readFloat(String name)
    {
        return Float.valueOf(this.readStr(name));
    }
    
    public String readStr(String name)
    {
        return this.map.get(name);
    }
    //</editor-fold>
    
    public boolean closeRead()
    {
        try 
        {
            this.reader.close();
            return true;
        } 
        catch (IOException e) 
        {
            System.out.println(e);
            System.out.println("[Engine]  Failure finishing a read.");
            return false;
        }
    }
}